<?php
declare (strict_types = 1);

namespace app\admin\controller;


use app\admin\model\Gcategory;
use app\admin\model\Goods;
use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class Good extends BaseController
{
    public function good()
    {
        $cate = Gcategory::test();
        $alist = \app\admin\model\Brand::pin();
        $cate_id = Request::get('cate_id','','trim,htmlspecialchars,strip_tags');
        $keywords= Request::get('keywords','','trim,htmlspecialchars,strip_tags');
        $recom=Request::get('recom','200','trim,htmlspecialchars,strip_tags');
        $maxtime= Request::get('maxtime','','trim,htmlspecialchars,strip_tags');
        $mintime=Request::get('mintime','','trim,htmlspecialchars,strip_tags');
        $maxjia= Request::get('maxjia','','trim,htmlspecialchars,strip_tags');
        $minjia= Request::get('minjia','','trim,htmlspecialchars,strip_tags');
        $if_show= Request::get('if_show',[],'trim,htmlspecialchars,strip_tags');
        $bid= Request::get('bid','','trim,htmlspecialchars,strip_tags');
        $data=[
            'cate_id'=>$cate_id,
            'keywords'=>$keywords,
            'recom'   =>$recom,
            'maxtime' =>$maxtime,
            'mintime' =>$mintime,
            'maxjia'  =>$maxjia,
            'minjia'  =>$minjia,
            'if_show' =>$if_show,
            'bid'     =>$bid,
            'order'       => $this->getSortOrder(Request::get()),
        ];


        $list=Goods::getGoodsList($data,input('get.'));
        View::assign($data);
        View::assign('cate',$cate);
        View::assign('alist',$alist);


        return  View::fetch('',['list'=>$list]);
    }

    protected function getSortOrder($get = []){
        // 实现排序
        //$get  = Request::get();
        $flag = '';
        if(isset($get['sort']) && 'desc' == $get['sort']){
            // 按时间倒序
            $flag = "↓";
            $get['sort'] = 'asc';
            $order = 'desc';
        }elseif(isset($get['sort']) && 'asc' == $get['sort']){
            // 按时间正序
            $flag = "↑";
            $order = 'asc';
            if(isset($get['sort']))   unset($get['sort']);
        }else{
            // 按goods_id 正序
            $order = '';
            $get['sort'] = 'desc';
        }
        View::assign('get',$get);
        View::assign('flag',$flag);

        return $order;
    }









    public function good2()
    {
        $keywords= Request::get('keywords');
        $recommended= Request::get('recommended');
        $if_show= Request::get('if_show');

        $where="1=1";
        if($keywords!=''){
            $where.=" and goods_name like '%{$keywords}%'";
        }
        if ($if_show!=""){
            $where.=" and if_show={$if_show}";
        }
        if ($recommended!=""){
            $where.=" and recommended={$recommended}";
        }
        $atime=\think\facade\Request::get('atime');
        if ($atime=='上架时间'){
            $atime='上架时间↓';
            $order="add_time desc";
        }elseif ($atime=='上架时间↓'){
            $atime='上架时间↑';
            $order="add_time ase";
        }else{
            $atime='上架时间';
            $order="goods_id";
        }

        $list=Db::table('ecm_goods')->order($order)->where($where)->select();
        View::assign('keywords',$keywords);
        View::assign('if_show',$if_show);
        View::assign('recommended',$recommended);
        View::assign('times',$atime);
        View::assign('list',$list);


        return  View::fetch();
    }

}
